package com.spring.pick2pick;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spring.pick2pick.common.PageHandler;
import com.spring.pick2pick.common.Util;
import com.spring.pick2pick.dao.CommonDao;
import com.spring.pick2pick.dao.LeagueDao;
import com.spring.pick2pick.model.LeagueItem;

@Controller
public class LeagueController {
	
	@Autowired
	LeagueDao leagueDao;
	@Autowired
	CommonDao commonDao;
	
	/** 리그 리스트 목록 */
	@RequestMapping(value = "listLeague", method = RequestMethod.GET)
	public String listLeague(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		int page = 1;
		int size = 10;
		String pageStr = request.getParameter("page");
		
		if (pageStr != null) {
			page = Integer.parseInt(pageStr);
		}
		
		model.addAttribute("toggle", commonDao.getCommonText("리그관리"));
		
		String idSportStr = request.getParameter("id");
		if (idSportStr == null || idSportStr.equals("")) {
			Util.getInstance().alertPage("파라미터가 넘어오지 못했습니다.", response);
			return null;
		}
		int idSport = Integer.parseInt(idSportStr);
		model.addAttribute("idSport", idSport);
		
		model.addAttribute("sportName", leagueDao.getSportName(idSport));
		model.addAttribute("displayCount", leagueDao.getDisplayCount(idSport));
		model.addAttribute("unDisplayCount", leagueDao.getUnDisplayCount(idSport));
		model.addAttribute("list", leagueDao.getLeagueList(page, size, idSport, "l.idLeague DESC"));
		model.addAttribute("page", PageHandler.getInstance());
		
		return "team/listLeague";
	}
	
	/** 리그 추가 */
	@RequestMapping(value = "addLeagueOk", method = RequestMethod.POST)
	public String addLeagueOk(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String idSportStr = request.getParameter("idSport");
		String addLeagueName = request.getParameter("addLeagueName");
		addLeagueName = addLeagueName.replace("&", "&amp;");
		addLeagueName = addLeagueName.replace("<", "&lt;");
		addLeagueName = addLeagueName.replace(">", "&gt;");
		String addSortKeyStr = request.getParameter("addSortKey");
		String addIsDisplayStr = request.getParameter("addIsDisplay");
		
		if (idSportStr == null || addLeagueName == null || addSortKeyStr == null || addIsDisplayStr == null ||
				idSportStr.equals("") || addLeagueName.equals("") || addSortKeyStr.equals("") || addIsDisplayStr.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
			return null;
		}
		
		int idSport = Integer.parseInt(idSportStr);
		int addSortKey = Integer.parseInt(addSortKeyStr);
		int addIsDisplay = Integer.parseInt(addIsDisplayStr);
		
		LeagueItem item = new LeagueItem();
		item.setIdSport(idSport);
		item.setLeagueName(addLeagueName);
		item.setSortKey(addSortKey);
		item.setIsDisplay(addIsDisplay);
		
		int result = leagueDao.addLeague(item);
		if (result > 0) {
			try {
				response.sendRedirect("listLeague?id=" + idSport);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			Util.getInstance().alertPage("추가에 실패하였습니다.", response);
		}
		
		return null;
	}
	
	/** 리그 수정 */
	@RequestMapping(value = "editLeagueOk", method = RequestMethod.POST)
	public String editLeagueOk(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String idLeagueStr = request.getParameter("editIdLeague");
		String idSportStr = request.getParameter("editIdSport");
		String leagueName = request.getParameter("editLeagueName" + idLeagueStr);
		leagueName = leagueName.replace("&", "&amp;");
		leagueName = leagueName.replace("<", "&lt;");
		leagueName = leagueName.replace(">", "&gt;");
		String sortKeyStr = request.getParameter("editSortKey" + idLeagueStr);
		String isDisplayStr = request.getParameter("editIsDisplay" + idLeagueStr);
		String page = request.getParameter("page");
		
		if (idLeagueStr == null || idSportStr == null || leagueName == null || sortKeyStr == null || isDisplayStr == null || 
				idLeagueStr.equals("") || idSportStr.equals("") || leagueName.equals("") || sortKeyStr.equals("") || isDisplayStr.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
			return null;
		}
		
		int idLeague = Integer.parseInt(idLeagueStr);
		int idSport = Integer.parseInt(idSportStr);
		int addSortKey = Integer.parseInt(sortKeyStr);
		int addIsDisplay = Integer.parseInt(isDisplayStr);
		
		LeagueItem item = new LeagueItem();
		item.setIdLeague(idLeague);
		item.setIdSport(idSport);
		item.setLeagueName(leagueName);
		item.setSortKey(addSortKey);
		item.setIsDisplay(addIsDisplay);
		
		int result = leagueDao.editLeague(item);
		if (result > 0) {
			try {
				String html = "<!doctype html>";
				html += "<head>";
				html += "<meta charset='utf-8'>";
				html += "<script>";
				html += "alert('수정되었습니다.');";
				html += "location.replace('listLeague?id=" + idSport + "&page=" + page + "');";
				html += "</script>";
				html += "</head>";
				html += "</html>";
				response.setCharacterEncoding("utf-8");
				PrintWriter out = response.getWriter();
				out.print(html);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			Util.getInstance().alertPage("수정에 실패하였습니다.", response);
		}
		
		return null;
	}

}
